package com.mashibing.juc.c_026_00_Interview.A1B2C3;

/**
 * wait await都是不占用CPU的
 * 自旋的写法，自旋的时候占用CPU
 * @author: Yuanzt
 * @Date: 2021/4/2
 */
public class T03_00_cas {

    //为防止取到其他值，所以定义成枚举
    enum ReadyToRun{T1,T2}

    static volatile ReadyToRun r = ReadyToRun.T1;

    public static void main(String[] args) {
        char[] aI = "1234567".toCharArray();
        char[] aC = "ABCDEFG".toCharArray();

        new Thread(()->{
            for (char c : aI) {
                while (r != ReadyToRun.T1){}
                    System.out.print(c);
                    r = ReadyToRun.T2;
            }
        },"t1").start();

        new Thread(()->{
            for (char c : aC) {
                while (r != ReadyToRun.T2){}
                System.out.print(c);
                r = ReadyToRun.T1;
            }
        },"t2").start();
    }
}
